package com.starblink.home.ui.cells

import android.annotation.SuppressLint
import android.graphics.Color
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.drawable
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.businessSuffixId
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.urllink.PullUpPageUtils
import com.starblink.android.basic.util.CoroutineUpdateTask
import com.starblink.android.basic.util.ImageUtils
import com.starblink.basic.ext.load
import com.starblink.basic.route.RoutePage
import com.starblink.android.basic.aroute.CommonRoute
import com.starblink.basic.util.ActivityStackManage
import com.starblink.home.R
import com.starblink.home.databinding.FragmentNavHomeBinding
import com.starblink.home.databinding.ItemHomeDiscountBinding
import com.starblink.home.databinding.LayoutHomeBrandItemBinding
import com.starblink.home.ui.NavHomeVM
import com.starblink.home.ui.bean.TopBrandOutletsBean
import com.starblink.home.ui.bean.TopSuperStoreBean
import com.starblink.home.ui.part.HomeApiResultControl
import com.starblink.library.widget.novice_guide.control.GuidePopStatusControl
import com.starblink.rocketreserver.FetchBrandCollectionListV2Query


/**
 * 顶部折扣
 */
class DiscountChildCell(
    binding: ItemHomeDiscountBinding,
    var parentVb: FragmentNavHomeBinding,
    var homeApiResultControl: HomeApiResultControl,
) :
    BaseBindingVH<TopSuperStoreBean, ItemHomeDiscountBinding>(binding) {
    @SuppressLint("SetTextI18n")
    override fun onBind(storeBean: TopSuperStoreBean, position: Int) {
        if (storeBean.data == null) {
            //骨架图
            binding.discountBg.gone()
            binding.layBgSkeleton.visible()
        } else {
            binding.discountBg.visible()
            binding.layBgSkeleton.gone()
            val model = storeBean.data!!
            val activity = ActivityStackManage.currentActivity()

            binding.imgLogo.visibleOrGone(false)
            binding.iDiscountImg.load(model.merchantWebVOF.imageUrl, onResourceReady = {
                binding.imgLogo.visibleOrGone(true)
                binding.imgLogo.load(model.merchantWebVOF.logo, model.merchantWebVOF.merchantName)
            }, placeholder = drawable(R.drawable.default_img_bg_1))
            //ImageUtils.loadImage(activity, model.merchantWebVOF.logo, binding.iDiscountBrandImg)
            binding.iDiscountPercentTxt.text = "${model.merchantWebVOF.discount ?: 0}%"
            binding.iDiscountBrandTxt.text = model.merchantWebVOF.merchantName
            binding.iDiscountBrandTxt.isSelected = true
            binding.iDiscount.click {
                CommonRoute.startStoreLinkWithCode(
                    model.merchantWebVOF.merchantWebNo?: model.merchantWebVOF.id,
                    model.merchantWebVOF.merchantWebUrl,
                    model.merchantWebVOF.couponCode ?: "",
                    model.merchantWebVOF.merchantDiscountInfo
                )
            }
            binding.iDiscount.trackData(
                page = SpmPageDef.Home080Page_10164,
                element = SpmElementDef.ELE_33009,
                params = mapOf("index" to position.toString()),
                businessSuffixID = businessSuffixId(model.merchantWebVOF),
                onlyHit = true
            )
        }
    }

}

/**
 * 顶部品牌 new
 */
class BrandChildCell(
    var viewModel: NavHomeVM,
    var parentVb: FragmentNavHomeBinding,
    binding: LayoutHomeBrandItemBinding,
) :
    BaseBindingVH<TopBrandOutletsBean, LayoutHomeBrandItemBinding>(
        binding
    ) {
    private companion object {
        const val TAG = "BrandChildCell"
    }

    @SuppressLint("SetTextI18n", "CheckResult")
    override fun onBind(outletsData: TopBrandOutletsBean, position: Int) {
        if (position == 0) {
            binding.brandItemTitle.visible()
            if (viewModel.brandOutletsNum > 0) {
                binding.brandLayText.text = "${viewModel.brandOutletsNum} "
                binding.brandLayTextSuffix.visible()
            } else {
                binding.brandLayTextSuffix.gone()
            }
            //引导
        } else {
            binding.brandItemTitle.gone()
        }
        if (outletsData.data == null) {
            //骨架图
            binding.layBg.gone()
            binding.imgBranItemSale.gone()
            binding.layBgSkeleton.visible()

        } else {
            if (position == 0) {
                GuidePopStatusControl.popBrandGuide(
                    appBar = parentVb.appBar,
                    refreshLayout = parentVb.page,
                    screenView = parentVb.layBrand.brandLay,
                    tarView = binding.brandItemLay,
                    bootView = binding.layBgP,
                    guideType = TAG
                )
            }
            binding.layBg.visible()
            binding.imgBranItemSale.visible()
            binding.layBgSkeleton.gone()
            val model: FetchBrandCollectionListV2Query.BrandList = outletsData.data!!
            //埋点曝光：首页-brand collecti33014
            binding.root.trackData(
                page = SpmPageDef.Home080Page_10164,
                element = SpmElementDef.Element_33014,
                params = mapOf(
                    "index" to "$position",
                    "brandName" to (model.brandVOF.name ?: "")
                ),
                businessSuffixID = businessSuffixId(model)
            )


            binding.textBrand.text = model.brandVOF.name
            binding.textUpTo.text = "${model.brandVOF.brandDiscount ?: "-"}%"
            binding.textItems.text = "${model.brandVOF.productNum} Items"
            //！！！！！！！！！！！！！！！！！！！！！！！
            //已经不需要做正片叠底了，因此这里新版的引导不要这样处理，直接加载图片即可
            binding.img1.load(
                model.brandVOF.brandImg, onResourceReady = {
                    binding.imgBr.load(model.brandVOF.logo)
                }, placeholder = drawable(R.drawable.default_img_bg_2)
            )

            binding.brandShowAll.run {
                trackData(
                    page = SpmPageDef.Home080Page_10164,
                    element = SpmElementDef.GdsSeemoreWidget,
                    onlyHit = true,
                    params = mapOf("module" to "brandOutlets")
                )
                click {
                    //跳转品牌View ALL页面
                    navigationTo(RoutePage.Brand.BRAND_ALL)
                }
            }

            binding.layBg.click {
                //跳转品牌二级页
                navDetails(model, position)
            }
        }
    }


    private fun navDetails(
        model: FetchBrandCollectionListV2Query.BrandList,
        position: Int,
    ) {
        //埋点：首页-brand collecti33014点击
        SpmTrackHandler.addSingleTrackData(
            SkAntEntity(
                spmCnt = GTrackerAssistUtils.fetchElementValue(
                    SpmPageDef.Home080Page_10164,
                    SpmElementDef.Element_33014
                ),
                params = mapOf(
                    "index" to "$position",
                    "brandName" to (model.brandVOF.name ?: "")
                ),
            )
        )

        navigationTo(RoutePage.Brand.BRAND_DETAIL) {
            withString(RoutePage.Brand.ARG_BRAND_ID, model.brandVOF.brandId.toString())
            withIntegerArrayList(RoutePage.Brand.ARG_EXPOSURE_BRAND_IDS, viewModel.brandIds)
            withBoolean(RoutePage.Brand.ARG_IS_DISCOUNT_BRAND, model.brandVOF.brandDiscount != null)
        }
    }

//        navigationTo(RoutePage.Brand.BRAND_DETAIL){
//            withString(RoutePage.Brand.ARG_BRAND_ID, "xxx")
//        }


}










